/**
 * HT1621 液晶屏驱动
 * Copyright (c) 2021 深圳市智辰科技有限公司
 * All rights reserved.
 */

#ifndef _DRIVER_HT1621_H_
#define _DRIVER_HT1621_H_

#include "nonos.h"


// 命令字
#define HT1621_CMD_SYS_DIS		0x00 // 0000 0000	// System reset default
#define HT1621_CMD_SYS_EN		0x01 // 0000 0001
#define HT1621_CMD_LCD_OFF		0x02 // 0000 0010	// System reset default
#define HT1621_CMD_LCD_ON		0x03 // 0000 0011
#define HT1621_CMD_TIMER_DIS	0x04 // 0000 0100
#define HT1621_CMD_WDT_DIS		0x05 // 0000 0101
#define HT1621_CMD_TIMER_EN		0x06 // 0000 0110
#define HT1621_CMD_WDT_EN		0x07 // 0000 0111
#define HT1621_CMD_TONE_OFF		0x08 // 0000 1000	// System reset default
#define HT1621_CMD_TONE_ON		0x09 // 0000 1001
#define HT1621_CMD_CLR_TIMER	0x0C // 0000 1100
#define HT1621_CMD_CLR_WDT		0x0E // 0000 1110
#define HT1621_CMD_XTAL_32K		0x14 // 0001 0100
#define HT1621_CMD_RC_256K		0x18 // 0001 1000	// System reset default
#define HT1621_CMD_EXT_256K		0x1C // 0001 1100
#define HT1621_CMD_BIAS2_DUTY2	0x20 // 0010 0000
#define HT1621_CMD_BIAS2_DUTY3	0x24 // 0010 0100
#define HT1621_CMD_BIAS2_DUTY4	0x28 // 0010 1000
#define HT1621_CMD_BIAS3_DUTY2	0x21 // 0010 0001
#define HT1621_CMD_BIAS3_DUTY3	0x25 // 0010 0101
#define HT1621_CMD_BIAS3_DUTY4	0x29 // 0010 1001
#define HT1621_CMD_TONE_4K		0x40 // 0100 0000
#define HT1621_CMD_TONE_2K		0x60 // 0110 0000
#define HT1621_CMD_IRQ_DIS		0x80 // 1000 0000	// System reset default
#define HT1621_CMD_IRQ_EN		0x88 // 1000 1000
#define HT1621_CMD_IRQ_F1		0xA0 // 1010 0000
#define HT1621_CMD_IRQ_F2		0xA1 // 1010 0001
#define HT1621_CMD_IRQ_F4		0xA2 // 1010 0010
#define HT1621_CMD_IRQ_F8		0xA3 // 1010 0011
#define HT1621_CMD_IRQ_F16		0xA4 // 1010 0100
#define HT1621_CMD_IRQ_F32		0xA5 // 1010 0101
#define HT1621_CMD_IRQ_F64		0xA6 // 1010 0110
#define HT1621_CMD_IRQ_F128		0xA7 // 1010 0111
#define HT1621_CMD_IRQ_TEST		0xE0 // 1110 0000
#define HT1621_CMD_IRQ_NORMAL	0xE3 // 1110 0011

// HT1621 驱动数据结构
typedef struct {
	// GPIO 驱动函数
	FUNC_GPIO_Out cs;
	FUNC_GPIO_Out wr;
	FUNC_GPIO_Out rd;
	FUNC_GPIO_Out dw;
	FUNC_GPIO_ReadIn dr;
} driver_ht1621_t;

/**
 * @brief 驱动初始化
 */
void ht1621_init(driver_ht1621_t *dev, FUNC_GPIO_Out cs, FUNC_GPIO_Out wr, FUNC_GPIO_Out rd, FUNC_GPIO_Out dw, FUNC_GPIO_ReadIn dr);

/**
 * @brief 发送一个或多个命令字
 * @param dev 驱动指针
 * @param cmds 命令字数组
 * @param len 命令字数组长度
 */
void ht1621_cmd(const driver_ht1621_t *dev, const uint8_t cmds[], int len);

/**
 * @brief 写入一个或多个数据(连续写)
 * @param dev 驱动指针
 * @param addr 地址
 * @param data 数据
 * @param len 数据长度
 */
void ht1621_write(const driver_ht1621_t *dev, uint8_t addr, const uint8_t data[], int len);


#endif // _DRIVER_HT1621_H_
